package com.coda.controller.user;

import com.coda.common.PageResult;
import com.coda.common.Result;
import com.coda.constant.MessageConstant;
import com.coda.constant.StatusConstant;
import com.coda.context.BaseContext;
import com.coda.pojo.UserWord;
import com.coda.pojo.Word;
import com.coda.pojo.dto.WordDTO;
import com.coda.service.WordService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


/**
 * 单词
 */
@RequestMapping("/user/word")
@RestController
public class WordController {

    @Autowired
    private WordService wordService;

    /**
     * 查询所有单词列表
     */
    @PostMapping("/all")
    public Result<PageResult> getWords(@RequestBody WordDTO wordDTO){
        PageResult words = wordService.getAllWords(wordDTO);
        return  Result.success(words);
    }

    /**
     * 查询当前用户已经默写的单词
     */
    @PostMapping("/dictationList")
    public Result<PageResult> getDictationList(@RequestBody WordDTO wordDTO) {
        PageResult words = wordService.getDictationList(wordDTO);
        return Result.success(words);
    }

    /**
     * 获取今天已经默写过的单词
     */
    @GetMapping("/dailyWord")
    public Result<List<Word>> getDailyWord() {
        UserWord userWord = new UserWord();
        userWord.setUserId(BaseContext.getCurrentId());
        userWord.setCreateTime(LocalDateTime.now());
        List<Word> list = wordService.getDailyWord(userWord);
        return Result.success(list);
    }

    /**
     * 获取需要默写的单词
     */
    // todo 没有根据单词设置匹配对应难度的单词
    @GetMapping("/getWord")
    public Result<Word> getWord() {
        Word word = wordService.getWord();
        return Result.success(word);
    }

    /**
     * 判断单词正确性
     */
    @GetMapping("/answer")
    public Result<String> answer(Integer id,String word){
        int result = wordService.answer(id,word);
        return result > 0 ? Result.success(StatusConstant.isCorrect, MessageConstant.ANSWER_SUCCESS) :
                Result.error(StatusConstant.notCorrect,MessageConstant.ANSWER_ERR);
    }
}
